//
//  ViewController.swift
//  ToDoList
//
//  Created by Qingke on 16/4/11.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class TaskListViewController: UITableViewController,TaskDetailDelegate{
    
    var category: Category!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = category.name
        
        //添加临时的任务
//        for index in 1...3 {
//            let task = Task()
//            task.name = "\(category.name)的任务\(index)"
//            category.tasks.append(task)
//            
//        }

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    func loadTask() {
//        let task1 = Task()
//        task1.name = "吃饭"
//        category.tasks.append(task1)
//        
//        let task2 = Task()
//        task2.name = "睡觉"
//        category.tasks.append(task2)
//        
//    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return category.tasks.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Mycell",forIndexPath: indexPath) as! MyCellTableViewCell
        
        let task = category.tasks[indexPath.row]
        configureCell(cell, task: task)
        return cell
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            let task = category.tasks[indexPath.row]
            let checkMarkLable = cell.viewWithTag(1001) as! UILabel
            //            if cell.accessoryType == .None {
            //                cell.accessoryType = .Checkmark
            //            } else {
            //                cell.accessoryType = .None
            //            }
            if checkMarkLable.text == "√" {
                checkMarkLable.text = ""
                task.IsChecked = false
            } else {
                checkMarkLable.text = "√"
                task.IsChecked = true
            }
        }
    }
    
    //    @IBAction func addTask(task: Task) {
    ////        let task = Task()
    ////        task.name = ""
    //        category.tasks.append(task)
    //
    //        let indexPath = NSIndexPath(forRow: category.tasks.count - 1 , inSection: 0)
    //        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    //
    ////        tableView.reloadData()
    //    }
    
    override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        category.tasks.removeAtIndex(sourceIndexPath.row)
        //
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        let navigationVC = segue.destinationViewController as! UINavigationController
        let addTaskVC = navigationVC.topViewController as! TaskDetailViewController
        
        if segue.identifier == "addTask" {
            addTaskVC.taskDetaildelegate = self
        } else if segue.identifier == "editTask" {
            let cell = sender as! UITableViewCell
            let indexPath = tableView.indexPathForCell(cell)
            //unwrap 解包
            if let indexPath = indexPath {
                let task = category.tasks[indexPath.row]
                addTaskVC.taskToEdit = task
                addTaskVC.taskDetaildelegate = self
            }

        }
    }
//点击+号调用的方法
    func addTaskViewController(viewcontroller: TaskDetailViewController, task: Task) {
        
        category.tasks.append(task)
        let indexPath = NSIndexPath(forRow: category.tasks.count - 1 , inSection: 0)
        tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
    }
//点击！调用的方法
    func editTaskViewController(controller: TaskDetailViewController,task: Task) {
        let index = category.tasks.indexOf(task)
        
        if let index = index {
            let indexPath = NSIndexPath(forRow: index, inSection: 0)
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCellTableViewCell
            configureCell(cell, task: task)
        }
    }
//configure 方法
    func configureCell(cell:MyCellTableViewCell,task: Task){
        cell.customLabel.text = task.name
        let checkMarkLabel = cell.viewWithTag(1001) as! UILabel
        //
        checkMarkLabel.textColor = view.tintColor
        if task.IsChecked {
            checkMarkLabel.text = "√"
        } else {
            checkMarkLabel.text = ""
        }
    }
    
//删除某一个row 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        let index = indexPath.row
        category.tasks.removeAtIndex(index)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
        
    }
}

















